var _Setting = function () {

  function Getter(setting){
    function getItem(key, defaultValue){
      let keys = key.split(".");
      let item =setting;
      for(let i=0;i<keys.length;i++){
        item=item[keys[i]];
        if(!item){return null;}
      }
      return item;
    }
    function getValue(key,defaultValue=null){
      let item = getItem(key);
      if(!item){return defaultValue;}
      return item["value"]
    }
    function getValueEn(key, defaultValue=null){
      let item = getItem(key);
      if(!item){return defaultValue;}
      return item["valueEn"]
    }
    setting["getItem"]=getItem;
    setting["getValue"]=getValue;
    setting["getValueEn"]=getValueEn;
  }
  let createSettingItem=()=>{
    let obj={};
    new Getter(obj);
    return obj;
  }
  let setting={};
  let loadMySetting = (type,cb,name="setting",typeName,subTypeName)=>{
    if (!setting[name]) {
      setting[name] = fetchSetting(name, typeName,subTypeName);
    }
    let result = setting[name];
    cb && cb(result);
    return result;
  }
  let fetchSetting = (name="setting",typeName="type",subTypeName="sub_type",) =>{
    let list = $$.listSync(name,{"project":DataUtils.get("dataType")});
    let setting = createSettingItem();
    (list || []).forEach(item => {
      let settingType = setting[item[typeName]];
      if (!settingType) {
        setting[item[typeName]] = createSettingItem();
        settingType = setting[item[typeName]];
      }
      let settingSubType = settingType[item[subTypeName]];
      if (!settingSubType) {
        settingType[item[subTypeName]] = {};
        settingSubType = settingType[item[subTypeName]];
      }
      settingSubType[item["key"]] = item;
    });
    return setting;
  }
  let loadDetailSetting = (type=DataUtils.get("dataType"))=>{
    let list = $$.listSync("detail_setting",{type});
    (list||[]).forEach(item =>{
      if (item["exclude"]){
        item["excludeArray"]=JSON.parse(item["exclude"]||'[]') ;
      }
      let certRates = (item["certRate"]||"").split(",")
      let rankNameCns = (item["rankNameCn"]||"").split(",")
      let rankNameEns = (item["rankNameEn"]||"").split(",")
      let rates = [];
      for(let i=0; i < certRates.length; i++){
        rates.push({"certRate":certRates[i], "rankNameCn":rankNameCns[i], "rankNameEn":rankNameEns[i], })
      }
      item["certRateArray"]=rates;

    })
    return list;
  };
  let loadCertType = (type=DataUtils.get("dataType"))=>{
    let list = $$.listSync("cert_type", {type,"status":1}, null,"area,mode,sortIndex");
    (list||[]).forEach(item =>{
      if(item["group"]){
        item["group"]=JSON.parse(item["group"]);
      }
    })
    return list;
  }
  return {
    loadSetting:(type,cb)=>{
      return loadMySetting(type,cb)
    },
    loadDetailSetting,
    loadCertType,
  }
}();

